<?php
/**
 * 链动会员模型关联使用示例
 */

// 使用示例：

// 1. 在控制器中使用
use addon\lian\model\LianMember;

class ExampleController 
{
    public function memberList()
    {
        $model = new LianMember();
        
        // 使用模型关联查询（推荐）
        $condition = [
            ['site_id', '=', 1],
            ['status', '=', 1]
        ];
        
        $result = $model->getLianMemberPageListWithRelation($condition, 1, 10);
        
        if ($result['code'] == 0) {
            // 查询成功
            $list = $result['data']['list'];
            foreach ($list as $item) {
                echo "会员ID: " . $item['member_id'] . "\n";
                echo "昵称: " . $item['nickname'] . "\n";
                echo "等级: " . $item['level_name'] . "\n";
                
                // 直推上级信息
                if ($item['pid_info']) {
                    echo "直推上级: " . $item['pid_info']['nickname'] . "\n";
                }
                
                // 链动上级信息
                if ($item['lian_pid_info']) {
                    echo "链动上级: " . $item['lian_pid_info']['nickname'] . "\n";
                }
                
                echo "总收益: " . $item['commission'] . "\n";
                echo "已发放: " . $item['give_commission'] . "\n";
                echo "冻结: " . $item['frozen_commission'] . "\n";
                echo "---\n";
            }
        }
    }
    
    public function memberDetail()
    {
        $model = new LianMember();
        $member_id = 123;
        
        // 获取单个会员详情（带关联）
        $result = $model->getLianMemberWithRelation($member_id);
        
        if ($result['code'] == 0) {
            $member = $result['data'];
            echo "会员详情:\n";
            echo "ID: " . $member['id'] . "\n";
            echo "会员ID: " . $member['member_id'] . "\n";
            echo "昵称: " . $member['member']['nickname'] . "\n";
            echo "等级: " . $member['level']['level_name'] . "\n";
        }
    }
    
    public function memberChildren()
    {
        $model = new LianMember();
        $member_id = 123;
        
        // 获取直推下级
        $directChildren = $model->getDirectChildren($member_id, 5);
        if ($directChildren['code'] == 0) {
            echo "直推下级:\n";
            foreach ($directChildren['data'] as $child) {
                echo "- " . $child['member']['nickname'] . "\n";
            }
        }
        
        // 获取链动下级
        $lianChildren = $model->getLianChildren($member_id, 5);
        if ($lianChildren['code'] == 0) {
            echo "链动下级:\n";
            foreach ($lianChildren['data'] as $child) {
                echo "- " . $child['member']['nickname'] . "\n";
            }
        }
    }
}

// 2. 直接使用 LianMemberModel
use addon\lian\model\sqlmodel\LianMemberModel;

class DirectModelExample
{
    public function advancedQuery()
    {
        $model = new LianMemberModel();
        
        // 复杂查询示例
        $members = $model->with(['member', 'level', 'parentMember'])
            ->where('status', 1)
            ->where('commission', '>', 100)
            ->order('commission desc')
            ->limit(10)
            ->select();
            
        foreach ($members as $member) {
            echo "高收益会员: " . $member->member->nickname . 
                 " 收益: " . $member->commission . 
                 " 等级: " . $member->level->level_name . "\n";
        }
        
        // 统计查询
        $totalCommission = $model->where('status', 1)->sum('commission');
        echo "总收益: " . $totalCommission . "\n";
        
        // 分组统计
        $levelStats = $model->with('level')
            ->where('status', 1)
            ->group('level_id')
            ->field('level_id, count(*) as member_count, sum(commission) as total_commission')
            ->select();
            
        foreach ($levelStats as $stat) {
            echo "等级 " . $stat->level_id . 
                 " 人数: " . $stat->member_count . 
                 " 总收益: " . $stat->total_commission . "\n";
        }
    }
}

// 3. 在现有的 LianMember 类中混合使用
/*
在 addon/lian/model/LianMember.php 中，你现在可以：

1. 保持原有的业务逻辑方法不变
2. 新增使用模型关联的方法
3. 在需要复杂查询时使用关联功能

例如：
public function someBusinessMethod() 
{
    // 原有的业务逻辑
    $result = $this->addLianMember($data);
    
    // 需要关联查询时
    $memberInfo = $this->getLianMemberWithRelation($member_id);
    
    return $result;
}
*/

// 4. 模型关联的优势
/*
1. 性能优化：
   - 预载入关联数据，避免 N+1 查询问题
   - 一次查询获取所有相关数据

2. 代码简洁：
   - 不需要手动编写复杂的 JOIN 查询
   - 自动处理关联关系

3. 类型安全：
   - ThinkPHP 的模型关联提供更好的类型提示
   - 减少手动拼接 SQL 的错误

4. 易于维护：
   - 关联关系定义清晰
   - 便于后续扩展和修改

5. 功能丰富：
   - 支持预载入、延迟加载
   - 支持关联统计、关联更新等高级功能
*/
